package main

import "fmt"

type Stack struct {
	top       int
	base      int
	stackSize int
	stackList []interface{}
}

//栈的初始化
func initStack(size int, arr []interface{}) *Stack {
	if size <= 0 {
		return new(Stack)
	}
	list := &Stack{
		top:       0,
		base:      0,
		stackSize: size,
		stackList: make([]interface{}, 0, size),
	}

	for i := range arr {
		list.stackList = append(list.stackList, arr[i])
		list.top++
	}
	return list
}
func (stack *Stack) push(val interface{}) {
	if stack.top-stack.base == stack.stackSize {
		fmt.Println("栈满了")
		return
	}

	stack.stackList = append(stack.stackList, val)
	stack.top++
}

func (stack *Stack) pop() interface{} {
	if stack.top == stack.stackSize {
		fmt.Println("栈空")
		return nil

	}
	val := stack.stackList[stack.top-1]
	stack.top--
	stack.stackList = append(stack.stackList[:stack.top])
	return val
}

//取栈顶元素
func (stack *Stack) getTop() interface{} {
	if stack.top == stack.base {
		fmt.Println("栈空")
		return nil
	}
	val := stack.stackList[stack.top-1]
	return val
}

func main() {
	data := []interface{}{
		"bala",
		"aa",
		12,
		45,
	}
	//var data2 []interface{}
	L := initStack(6, data)
	fmt.Println(L)

	L.push("99")
	fmt.Println(L)

	fmt.Printf("栈顶元素为:%s\n", L.getTop())
	fmt.Println(L.pop())
	fmt.Println(L)
	// test

}
